package cs236369.hw5;

import java.sql.Date;

public class Team extends Post {

	/**
	 * Creates a new team with no users members in the team.
	 * @param id
	 * @param owner
	 * @param creationTime
	 * @param name
	 * @param capacity
	 * @param content
	 * @param latitude
	 * @param longitude
	 * @param skills
	 * @param limit The maximum number of people can join the team
	 * @throws RescueMeException
	 */
	public Team(int id, String owner, Date creationTime, String name, int capacity,
			String content, double latitude,
			double longitude, Skill[] skills, int limit) throws RescueMeException {
		super(id, owner, creationTime, name, capacity, content, latitude,
				longitude, skills);
		if(limit < 1) {
			throw new RescueMeException("illigal parameter", "Team limit must be a positive integer");
		}
		this.limit = limit;
	}
	
	/**
	 * Creates team with users in it.
	 * @param id
	 * @param owner
	 * @param creationTime
	 * @param name
	 * @param capacity
	 * @param content
	 * @param latitude
	 * @param longitude
	 * @param skills
	 * @param limit
	 * @param members
	 * @throws RescueMeException
	 */
	public Team(int id, String owner, Date creationTime, String name, int capacity,
			String content, double latitude,
			double longitude, Skill[] skills, int limit, User[] members) throws RescueMeException {
		this(id, owner, creationTime, name, capacity, content, latitude, longitude, skills, limit);
		this.members = members;
	}
	
	public int getLimit() { return limit; }
	
	/**
	 * 
	 * @return true if the team is not full yet
	 */
	public boolean canJoin() {
		return (limit - members.length) > 0;
	}
	
	/**
	 * 
	 * @param username
	 * @return true if the user is a member of the team
	 */
	public boolean isMember(String username) {
		for(User user : members) {
			if(username.equals(user.getUserName())) { return true; }
		}
		return false;
	}
	
	/**
	 * 
	 * @return Array of members.
	 */
	public User[] getMembers() { 
		if(members == null) {
			members = new User[0];
		}
		return members;
	}
	
	private int limit;
	private User[] members;
}
